#include "constants.h";

XfMissionChapterExecute={
	private ["_chapter_succeeded"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 1                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Starting Chapter","_last_random_reinforcement","_random_reinf_diff","_reinforcement_power"];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Mission canceled!"];};

	_random_town = DM_Chapter_Location;
	_random_town_pos = _random_town select XF_MAP_LOCATION_CENTER;
	_random_town_pos_size = _random_town select XF_MAP_LOCATION_SIZE;
	
	_retake_updated = false;
	
	diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Entering the loop",[], Time];
	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 10;
		_enemy_counts = ([_random_town_pos,_random_town_pos_size * 2,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
		if (_enemy_counts < 3 && !_retake_updated) then {
			["RetakeMike2","", "","",[],"Succeeded"] call XfMissionTask;
			_retake_updated = true;
		};
		_enemy_counts = ([_random_town_pos,_random_town_pos_size,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
		if (_enemy_counts < 1) then {
			DM_Mission_Chapter_CanRun = false;
			_chapter_succeeded = true;
			sleep 30;
		};
	};
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Mission canceled!"];
	};

	// Just in case a reinforcement is already on the way.
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_random_town","_random_town_pos","_random_town_pos_size","_troops_power","_troops_coeff","_grpSpecOps","_i","_l"];
	diag_log Format["(%1)[%2]: %3; %4 %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Starting Initialization:",DM_Mission_Name];
	
	// Units positions
	_mike_camp_units =[
		[[17450.6,6047.5,1067.93],238.683],
		[[17453.3,6033.65,1065.48],244.671],
		[[17454.5,6036.35,1065.84],254.12],
		[[17468.9,6030.79,1068.88],250.957],
		[[17471.1,6023.1,1069.35],230.282],
		[[17504.5,6038.77,1068.01],132.991],
		[[17503.4,6047.26,1071.77],186.121],
		[[17506.9,6045.77,1071.77],103.96],
		[[17503.1,6050.76,1069.19],265.472],
		[[17500.8,6064.59,1069.89],215.875],
		[[17490.6,6062.75,1069.15],305.852],
		[[17501.4,6071.95,1069.89],272.482],
		[[17496.9,6072.52,1069.89],213.152],
		[[17492.5,6075.32,1069.16],12.5253],
		[[17504,6097.47,1069.19],86.0678],
		[[17506.4,6097.48,1072],73.6435],
		[[17458.9,6098.2,1071.78],352.67],
		[[17454.4,6098.1,1071.78],277.534],
		[[17454.8,6097.61,1068.92],273.85],
		[[17465.1,6072.27,1069.24],97.3857],
		[[17468.6,6054.32,1069.23],82.3125],
		[[17466.9,6062.2,1069.89],66.8512],
		[[17465.4,6068.56,1069.89],353.225],
		[[17460.1,6067.88,1069.89],355.953],
		[[17464,6060.99,1069.46],151.523],
		[[17455.6,6062.14,1071.92],260.714],
		[[17458,6055.8,1071.92],177.679],
		[[17467.7,6057.14,1071.92],183.492],
		[[17467.4,6061.66,1071.92],85.1341],
		[[17465.9,6068.85,1071.92],3.50496]
	];

	// Gate vehicle
	_mike_gate_vec = [[17449.6,6023.8,1064.27],248.932];

	_mike_aa_static = [
		[[17478.6,6106.01,1069.15],179.791],
		[[17462,6085.54,1069.14],92.2778],
		[[17488.8,6029.94,1067.28],347.282]
	];
		
	_random_town = [];
	_random_town_pos = [];
	_parameters = [d_map_locations];
	_parameters set [count _parameters, [XF_MAP_LOCATION_UNIQUENAME,"==","Mike2"]];
	_eligible_towns = _parameters call XfTableFilter;
	if ((count _eligible_towns) <= 0) exitWith {"STR_MF_RANDOMSITEFAILED" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};
	_random_town = _eligible_towns select 0;
	_random_town_pos = _random_town select XF_MAP_LOCATION_CENTER;
	_random_town_pos_size = _random_town select XF_MAP_LOCATION_SIZE;
	_random_town_pos_name = _random_town select XF_MAP_LOCATION_DISPLAYNAME;
	
	DM_Chapter_Location = _random_town;
	DM_Chapter_Position = _random_town_pos;
	sleep 15;
	
	_machifazar_pos = [17077.2,5945.98,0];
	_crossroad_pos = [17966.2,5837.48,0];
	
	[false,["chapter1_mike2", _random_town_pos, "ELLIPSE", "ColorRed", [_random_town_pos_size,_random_town_pos_size],"",0,"","BDiagonal",0.5]] call XfMissionMarker;
	[true,["chapter1_machifazar", _machifazar_pos, "ELLIPSE", "ColorRed", [150,150],"",0,"","",0.000001]] call XfMissionMarker;
	[true,["chapter1_crossroad", _crossroad_pos, "ELLIPSE", "ColorRed", [60,60],"",0,"","",0.000001]] call XfMissionMarker;
	
	_units = [];
	for [{_l = 0}, {_l < 4}, {_l = _l + 1 }] do {
		_grpSoldiers = [d_enemy_side,[_random_town_pos,50],["chapter1_mike2","noveh","aware","buildings","fortify"],floor(random(5))] call XfMissionCreateFootmenGroup;
		_units = [_units, (units _grpSoldiers)] call XfArrayPushStack;
	};
	
	_grpSoldiers = [d_enemy_side,[_machifazar_pos,100],["chapter1_machifazar","noveh","aware","buildings"],floor(random(5))] call XfMissionCreateFootmenGroup;
	_grpSoldiers = [d_enemy_side,[_crossroad_pos,60],["chapter1_crossroad","noveh","aware","buildings"],floor(random(5))] call XfMissionCreateFootmenGroup;
		
	_fnc_KeepInPlace = {
		_unit = _this select 0;
		_unit setUnitPos "Middle";
		_initial_pos = getPos _unit;
		doStop _unit;
		while { alive _unit } do {
			if ((_unit distance _initial_pos) > 2) then { _unit doMove _initial_pos; };
			sleep 1.23456;
		};
	};

	_i = 0;
	{
		doStop _x;
		if (_i < count _mike_camp_units) then {
			_pos = _mike_camp_units select _i;
			_x setPosASL (_pos select 0);
			_x setDir (_pos select 1);
			_i = _i + 1;
			[_x] spawn _fnc_KeepInPlace;
		} else {
			[_random_town_pos, _random_town_pos_size] call XfGetRanPointCircleBig;
		};
	} foreach _units;
	
	_enemy_side_char = switch (d_enemy_side) do {case "EAST": {"E"};case "WEST": {"W"};case "GUER": {"G"};case "CIV": {"G"}; default{"E"};};
	_vehicle_array = missionNamespace getVariable format ["d_veh_a_%1",_enemy_side_char];
	
	_grp_statics = [d_enemy_side] call x_creategroup;
	for [{_l = 0}, {_l < count _mike_aa_static}, {_l = _l + 1 }] do {
		_vec_type = ((_vehicle_array select XF_VEHICLE_TYPES_STATICAA) call XfRandomArrayVal);
		_pos_vec = ((_mike_aa_static select _l) select 0);
		_pos_vec set [2, 0];
		_dir_vec = ((_mike_aa_static select _l) select 1);
		diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Create Static",[_vec_type, _pos_vec, _dir_vec]];
		[_vec_type,false,_grp_statics,[_pos_vec,0],["chapter1_mike2","combat","RED"]] call XfMissionCreateUnitGroup;
		_vec = (units _grp_statics) select _l;
		_vec setPos _pos_vec;
		_vec setDir _dir_vec;
	};
	
	_vec_type = ((_vehicle_array select XF_VEHICLE_TYPES_LIGHTWHEEL) call XfRandomArrayVal);
	_grpVec = [_vec_type,false,d_enemy_side,[(_mike_gate_vec select 0),(_mike_gate_vec select 1)],["chapter1_mike2","combat","RED","fortify"]] call XfMissionCreateUnitGroup;
	_vec = vehicle (leader _grpVec);
	_vec lockDriver true;
	_driver = driver _vec;
	doStop _driver;
	_vec setPosASL (_mike_gate_vec select 0);
	_vec setDir (_mike_gate_vec select 1);
	
	[["%1\n\n%2","STR_MF_M_HAMMER1_1_NAME","STR_MF_M_HAMMER1_1_INTRO"],[],8,10,"PLAIN"] call XfMissionDisplayScreen;	
	[false,["chapter1_mike_2", _random_town_pos, "ICON", "ColorBlue", [0.8,0.8],"STR_MF_M_HAMMER1_1_TASK",0,"hd_flag","",0.5]] call XfMissionMarker;

	// This indicates where the mission takes place
	[_random_town_pos,_random_town_pos_size,_random_town_pos_name] call XfMissionCurrentPosition;
	["RetakeMike2","STR_MF_M_HAMMER1_1_TASK", "STR_MF_M_HAMMER1_1_TASK_DESC","",[],"Created"] call XfMissionTask;
};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSuccess","Starting"];
	"STR_MF_M_HAMMER1_1_SUCCESS" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterFailure","Starting"];
	"STR_MF_MISSIONFAILURE1" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	[] call XfMissionCleanMarkers;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;